﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace Super.Core.CodeHelper
{
    public class CodeCommon
    {
        #region 缩进间隔
        /// <summary>
        /// 缩进间隔
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string Space(int num)
        {
            StringBuilder str = new StringBuilder();
            for (int n = 0; n < num; n++)
            {
                str.Append("\t");
            }
            return str.ToString();
        }
        #endregion

        #region 转换 数据库字段类型 为 c#类型

        /// <summary>
        /// 转换【数据库字段类型】 =》为【c#类型】
        /// </summary>
        /// <param name="dbtype">数据库字段类型</param>
        /// <returns>c#类型</returns>		
        public static string DbTypeToCS(string dbtype)
        {
            string CSType = "string";
            /*
            if (File.Exists(datatypefile))
            {
                datatype = new LTP.Utility.INIFile(datatypefile);
                string val = datatype.IniReadValue("DbToCS", dbtype.ToLower().Trim());
                if (val == "")
                {
                    CSType = dbtype.ToLower().Trim();
                }
                else
                {
                    CSType = val;
                }
            }
             */
            return CSType;

        }
        #endregion

        #region  得到表的字段List对象信息
        /// <summary>
        /// 将【字段信息表数据DataTable】转为 【所有字段对象数组List-ColumnInfo】
        /// </summary>
        public static List<ColumnInfo> GetColumnInfos(DataTable dt)
        {
            List<ColumnInfo> keys = new List<ColumnInfo>();
            if (dt != null)
            {
                ArrayList colexist = new ArrayList();//是否已经存在
                ColumnInfo key;
                foreach (DataRow row in dt.Rows)
                {
                    string Colorder = row["Colorder"].ToString();
                    string ColumnName = row["ColumnName"].ToString();
                    string TypeName = row["TypeName"].ToString();
                    string isIdentity = row["IsIdentity"].ToString();
                    string IsPK = row["IsPK"].ToString();
                    string Length = row["Length"].ToString();
                    string Preci = row["Preci"].ToString();
                    string Scale = row["Scale"].ToString();
                    string cisNull = row["cisNull"].ToString();
                    string DefaultVal = row["DefaultVal"].ToString();
                    string DeText = row["DeText"].ToString();

                    key = new ColumnInfo();
                    key.Colorder = Colorder;
                    key.ColumnName = ColumnName;
                    key.TypeName = TypeName;
                    key.IsIdentity = (isIdentity == "√") ? true : false;
                    key.IsPK = (IsPK == "√") ? true : false;
                    key.Length = Length;
                    key.Preci = Preci;
                    key.Scale = Scale;
                    key.cisNull = (cisNull == "√") ? true : false;
                    key.DefaultVal = DefaultVal;
                    key.DeText = DeText;

                    if (!colexist.Contains(ColumnName))
                    {
                        keys.Add(key);
                        colexist.Add(ColumnName);
                    }
                }
                return keys;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 将【所有字段对象数组List-ColumnInfo】转为 【字段信息表数据DataTable】
        /// </summary>        
        public static DataTable GetColumnInfoDt(List<ColumnInfo> collist)
        {
            DataTable cTable = new DataTable();
            cTable.Columns.Add("colorder");
            cTable.Columns.Add("ColumnName");
            cTable.Columns.Add("TypeName");
            cTable.Columns.Add("Length");
            cTable.Columns.Add("Preci");
            cTable.Columns.Add("Scale");
            cTable.Columns.Add("IsIdentity");
            cTable.Columns.Add("isPK");
            cTable.Columns.Add("cisNull");
            cTable.Columns.Add("defaultVal");
            cTable.Columns.Add("deText");

            foreach (ColumnInfo col in collist)
            {
                DataRow newRow = cTable.NewRow();
                newRow["colorder"] = col.Colorder;
                newRow["ColumnName"] = col.ColumnName;
                newRow["TypeName"] = col.TypeName;
                newRow["Length"] = col.Length;
                newRow["Preci"] = col.Preci;
                newRow["Scale"] = col.Scale;
                newRow["IsIdentity"] = (col.IsIdentity) ? "√" : "";
                newRow["isPK"] = (col.IsPK) ? "√" : "";
                newRow["cisNull"] = (col.cisNull) ? "√" : "";
                newRow["defaultVal"] = col.DefaultVal;
                newRow["deText"] = col.DeText;
                cTable.Rows.Add(newRow);
            }
            return cTable;
        }
        #endregion



        #region 该数据类型是否加单引号

        /// <summary>
        /// 该数据类型是否加单引号
        /// </summary>
        /// <param name="columnType">数据库类型</param>
        /// <returns></returns>
        public static bool IsAddMark(string columnType)
        {
            bool isadd = false;
            /*
            if (File.Exists(datatypefile))
            {
                datatype = new LTP.Utility.INIFile(datatypefile);
                string val = datatype.IniReadValue("IsAddMark", columnType.Trim().ToLower());
                if (val != "")
                {
                    isadd = true;
                }
            }
             */
            return isadd;
        }
        #endregion


    }
}
